Imports EnvDTE
Imports EnvDTE80

Public Class AvailableItems

    Private _VStudio As DTE2
    Private _Instance As AddIn
    Private _Project As Project
    Private _Tool As BaseTool
    Private _Items As Generic.List(Of String)
    Private WithEvents _Worker As ComponentModel.BackgroundWorker

    Public Sub New(ByVal vstudio As DTE2, ByVal instance As AddIn, ByVal project As EnvDTE.Project)
        Me.InitializeComponent()
        _VStudio = vstudio
        _Instance = instance
        _Project = project
        _Tool = ToolFactory.GetFromProject(project)
        _Worker = New ComponentModel.BackgroundWorker

        FormHeaderText.Text = String.Format(FormHeaderText.Text, _Project.Name)
    End Sub

    Private Sub AvailableItems_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        StartUpdateList()
    End Sub

    Private Sub FillItems()
        For Each item As String In _Items
            AvailableItemsCheck.Items.Add(item, False)
        Next
    End Sub

    Private Sub ClearItems()
        AvailableItemsCheck.Items.Clear()
    End Sub

    Private Sub CheckAllItemsClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckAllItems.Click
        For int As Integer = 0 To AvailableItemsCheck.Items.Count - 1
            AvailableItemsCheck.SetItemChecked(int, True)
        Next
    End Sub

    Private Sub CreateItemsClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateItems.Click
        Try
            Trace.WriteLine("Loading items to checked listbox")
            Dim items As System.Windows.Forms.CheckedListBox.CheckedItemCollection
            items = AvailableItemsCheck.CheckedItems

            Trace.WriteLine("Finding project-location")
            Dim projectFileLocation As String = _Project.FullName
            Dim projectFile As New System.IO.FileInfo(projectFileLocation)
            Dim fileDirectory As String = projectFile.Directory.FullName
            Trace.WriteLine("Result: " & projectFileLocation)
            Trace.WriteLine("Outputfolder: " & fileDirectory)

            For Each item As Object In items
                Try

                    Dim fileName As String = AvailableItemsCheck.GetItemText(item)
                    Dim fullFilePath As String = fileDirectory + "\" + fileName

                    Trace.WriteLine("Adding file " & fileName & " to " & fullFilePath)
                    If Not System.IO.File.Exists(fullFilePath) Then
                        System.IO.File.CreateText(fullFilePath).Close()
                    End If
                    Dim newItem As ProjectItem = _Project.ProjectItems.AddFromFile(fullFilePath)
                    If AutoDownload.Checked Then
                        Trace.WriteLine("Downloading file: " & fileName)
                        _Tool.DownloadItem(newItem)
                    End If
                Catch ex As Exception

                    MsgBox("Error adding file, " & ex.Message)
                    Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace)

                End Try
            Next

            Me.Close()
        Catch ex As Exception
            MsgBox("Error saving files, " & ex.Message)
            Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace)
        End Try
    End Sub

    Private Sub UpdateListClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateList.Click
        StartUpdateList()
    End Sub

    Sub StartUpdateList()
        LockForm()
        _Worker.RunWorkerAsync()
    End Sub

    Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _Worker.DoWork
        Try
            Dim result As Object = _Tool.AvailableItems(_Project)
            e.Result = result
        Catch ex As Exception
            Trace.WriteLine("Error loading items: " & ex.Message)
        End Try
    End Sub

    Private Sub WorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles _Worker.RunWorkerCompleted
        Try
            Dim result As Object = e.Result
            If result Is Nothing Then
                ClearItems()
            Else
                _Items = CType(result, Global.System.Collections.Generic.List(Of String))
                ClearItems()
                FillItems()
            End If
        Catch
        End Try
        UnlockForm()
    End Sub

    Private Sub LockForm()
        Me.UpdateList.Enabled = False
        Me.CheckAllItems.Enabled = False
        Me.CreateItems.Enabled = False
        Me.AutoDownload.Enabled = False
        Me.AvailableItemsCheck.Enabled = False
    End Sub

    Private Sub UnlockForm()
        Me.UpdateList.Enabled = True
        Me.CheckAllItems.Enabled = True
        Me.CreateItems.Enabled = True
        Me.AutoDownload.Enabled = True
        Me.AvailableItemsCheck.Enabled = True
    End Sub

    Private Sub AvailableItems_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If _Worker.IsBusy Then
            e.Cancel = True
        End If
    End Sub

End Class